package com.umgsai.wx.backend.test;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.umgsai.wx.backend.BaseTest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
public class FixDataHotel extends BaseTest {


	@Data
	public static class NewExcelData {

		@ExcelProperty(value = "hotelOrderId", index = 0)
		private String hotelOrderId;

		@ExcelProperty(value = "appointAmount", index = 1)
		private BigDecimal appointAmount;

		@ExcelProperty(value = "invoiceAmount", index = 2)
		private BigDecimal invoiceAmount;

		@ExcelProperty(value = "realGetAmount", index = 3)
		private BigDecimal realGetAmount;

		@ExcelProperty(value = "originAmount", index = 4)
		private BigDecimal originAmount;

		@ExcelProperty(value = "rmbAmount", index = 5)
		private BigDecimal rmbAmount;

		@ExcelProperty(value = "supplierOrderId", index = 6)
		private String supplierOrderId;

		@ExcelProperty(value = "totalProductPrice", index = 7)
		private BigDecimal totalProductPrice;

		@ExcelProperty(value = "totalProductPriceCNY", index = 8)
		private BigDecimal totalProductPriceCNY;

		@ExcelProperty(value = "totalPayPrice", index = 9)
		private BigDecimal totalPayPrice;

		@ExcelProperty(value = "totalCostPrice", index = 10)
		private BigDecimal totalCostPrice;

		@ExcelProperty(value = "unitAmount", index = 11)
		private BigDecimal unitAmount;
	}

	@Data
	public static class UnitAmountData {

		@ExcelProperty(value = "hotelOrderId", index = 0)
		private String hotelOrderId;

		@ExcelProperty(value = "appointAmount", index = 1)
		private BigDecimal appointAmount;

	}

	@Data
	public static class ExcelDataListener extends AnalysisEventListener<NewExcelData> {

		private List<NewExcelData> excelDataList = Lists.newArrayList();

		@Override
		public void invoke(NewExcelData data, AnalysisContext context) {

			excelDataList.add(data);
		}

		@Override
		public void doAfterAllAnalysed(AnalysisContext context) {
			//读取结束会调用该方法
			System.out.println("end...");
		}
	}


	@Data
	public static class UnitAmountDataListener extends AnalysisEventListener<UnitAmountData> {

		private List<UnitAmountData> unitAmountDataList = Lists.newArrayList();

		@Override
		public void invoke(UnitAmountData data, AnalysisContext context) {

			unitAmountDataList.add(data);
		}

		@Override
		public void doAfterAllAnalysed(AnalysisContext context) {
			//读取结束会调用该方法
			System.out.println("end...");
		}
	}


	@Test
	public void test() throws Exception {
		String excelPath = "D:\\NewExcelData.xlsx";
		ExcelDataListener excelDataListener = new ExcelDataListener();
		EasyExcel.read(excelPath, NewExcelData.class, excelDataListener).sheet().doRead();
		List<NewExcelData> excelDataList = excelDataListener.getExcelDataList();

		 excelPath = "D:\\unitAmount.xlsx";
		UnitAmountDataListener unitAmountDataListener = new UnitAmountDataListener();
		EasyExcel.read(excelPath, UnitAmountData.class, unitAmountDataListener).sheet().doRead();
		List<UnitAmountData> unitAmountDataList = unitAmountDataListener.getUnitAmountDataList();

		Map<String, Object> map = Maps.newHashMap();
		for (UnitAmountData unitAmountData : unitAmountDataList) {
			map.put(unitAmountData.getHotelOrderId(), unitAmountData.getAppointAmount());
		}

		for (NewExcelData newExcelData : excelDataList) {
			// TotalProductPrice产品币种总卖价  --> rmb产品为appointAmount  外币产品为 originPrice
			// TotalProductPriceCNY产品人民币总卖价  --> appointAmount
			// TotalPayPrice支付币种总卖价  --> appointAmount
			// totalCostPrice产品总底价  ----> originPrice
//			log.info("supplierOrderId={} totalProductPrice:{} totalProductPriceCNY:{} totalPayPrice:{} totalCostPrice:{}",
//					newExcelData.getSupplierOrderId(), newExcelData.getTotalProductPrice(), newExcelData.getTotalProductPriceCNY(),
//					newExcelData.getTotalPayPrice(), newExcelData.getTotalCostPrice());

//			System.out.printf("update ord_hotelgrouporderinfo set TotalProductPrice = %s, TotalProductPriceCNY = %s, TotalPayPrice = %s, totalCostPrice = %s where orderId = %s;\n",
//					newExcelData.getTotalProductPrice(), newExcelData.getTotalProductPriceCNY(), newExcelData.getTotalPayPrice(), newExcelData.getTotalCostPrice(), newExcelData.getSupplierOrderId());

//			System.out.printf("update ord_paymentinfo set InvoiceAmount = %s where OrderId = %s;\n", newExcelData.getInvoiceAmount(), newExcelData.getSupplierOrderId());

			System.out.printf("INSERT INTO db.queue" +
					"(" +
					"orderid," +
					"send_type," +
					"process_status," +
					"process_time," +
					"retry_times," +
					"shardid," +
					"operate_type)" +
					"VALUES" +
					"(%s,1,0,current_timestamp(),0,%d,1);\n", newExcelData.getSupplierOrderId(), (Long.parseLong(newExcelData.getSupplierOrderId()) % 64));

			newExcelData.setUnitAmount(map.get(newExcelData.getHotelOrderId()) == null ? BigDecimal.ZERO : (BigDecimal) map.get(newExcelData.getHotelOrderId()));
		}
		String newExcelPath = "D:\\NewExcelData111111116.xlsx";
		EasyExcel.write(newExcelPath, NewExcelData.class).sheet("sheet").doWrite(excelDataList);

	}
}
